package tp.pr2;

import java.util.Scanner; //para poder leer la cadena de entrada en el runGame

public class Game
{

	private Room currentRoom;
	private Map mapa;
	private Player friki;
	

	//nuevo constructor
	public Game(Map map, Room initialRoom)
	{
		this.mapa=map;
		this.currentRoom=initialRoom;
		this.friki=new Player();
	}
	
	//metodos
	
	public Room getCurrentRoom()
	{
		return this.currentRoom;
	}
	
	public void setCurrentRoom(Room currentRoom)
	{
		this.currentRoom=currentRoom;
	}


	
	protected void changeRoom(Directions direction)
	{
		Door auxiliar = mapa.getDoor(currentRoom, direction); 
		
		if (auxiliar==null)
		{
			System.out.println("What the hell am I supposed to do going to " + direction.toString() + " ?");
		}
		else
		{
			if (auxiliar.isOpen())
			{
				if ((auxiliar.getOrigen()==this.currentRoom)||(auxiliar.getBidi())) {
				System.out.println("...mooving to " + direction.toString());
				friki.addHealth(-5);
				}
				else {
					System.out.println("Impossible to go through the door from this side.");
				}
				if (auxiliar.getDir().getOpuesto()==direction && auxiliar.getBidi())
				{
					this.currentRoom=auxiliar.getOrigen();
				}
				else
				{
					this.currentRoom=auxiliar.getDestino();///GO south go east go north go west go west
					
				}
			}
			else
			{
				System.out.println("There is a door in the " + direction.toString() + ", but it is closed.");
			}
		}

	}

	protected boolean processCommand(Command command)
	{
		boolean termina = false;
	//	System.out.print("> ");
		if (command.isValid())
		{
			if (command.getVerb() == VerbCommands.QUIT)
			{
				termina = true;
			//	friki.getStatus();
			}
			else if (command.getVerb() == VerbCommands.HELP)
			{
				System.out.println(Parser.getHelp());	
			}
			else if (command.getVerb() == VerbCommands.EXAMINE)
			{
				mensInicial();
			}
			else if (command.getVerb() == VerbCommands.LOOK)
			{
				String id=command.getIdItem();
				if (id==null)
				System.out.println(this.friki.showItems());
				else
				{
					if (this.friki.getItem(id)==null)
					{
						System.out.println("There is no "+id+" in your inventory.");
					}
					else
						System.out.println(this.friki.getItem(id));
				}
				
			}
			else if (command.getVerb() == VerbCommands.PICK)
			{
				if ( this.currentRoom.existsItem(command.getIdItem()) )
				{
					this.currentRoom.pickItem(friki,command.getIdItem());	
				}
			}
			else if (command.getVerb() == VerbCommands.USE)
			{
				
				String id=command.getIdItem().toLowerCase();
				
				Item item=friki.getItem(id);
				if (item==null)
				{
					System.out.println("Someone stole your "+ id+ ".");
				}
				else
				{
					item.use(this.friki, this.currentRoom);
					
				}
			}
			
			else
			{
				Room z=this.currentRoom;
				this.changeRoom(command.getDirection());
				if (this.currentRoom!=z) {
				System.out.println(this.currentRoom.getDescription());
				if (!this.currentRoom.isExit()) this.friki.getStatus();
				}
				termina = this.currentRoom.isExit();
			}
		}

		return termina;
	}
	
	public void mensInicial()
	{
		System.out.println(this.currentRoom.getDescription());
		this.friki.getStatus();
	}
	public void mensFinal()
	{
		
		System.out.println("GAME OVER \n Thank you for playing, goodbye.");
		this.friki.getStatus();

	}

	public void runGame()
	{
		boolean endGame = false;
		String cadena;
		Command comando;
		Scanner entrada = new Scanner(System.in);
		// bucle principal de ejecucion del juego
		mensInicial();
		do
		{
			
			System.out.print("> ");
			cadena = entrada.nextLine();
			
			comando = Parser.nextCommand(cadena);

			if (comando != null)
			{
				endGame = processCommand(comando);

				if (!comando.isValid())
					System.out.println("What?");
			}
			if (friki.dead()) {
				System.out.println("You are dead.");
				break;
			}
		} while (!endGame);
		this.mensFinal();
	}
}
